package com.sirdc.modules.sys.service;

import com.sirdc.modules.sys.filter.NodeNewschedulesModel;
import com.sirdc.modules.utils.StringUtils;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import static net.huashitong.Project.util.HolidayUtils.getScheduleActiveDate;
import static net.huashitong.Project.util.PersDateUtils.getDate;

/**
 * 计划开始时间与计划结束时间推算
 */
@Service
public class PlanTimeReckonService {

    public List<NodeNewschedulesModel> reckonPlanTime(List<NodeNewschedulesModel> models, Map<String, Object> preOfNodeMap) throws ParseException {
        Map<String, String> finishBigNodeMap = new TreeMap<>();
        Map<String, String> finishSmallNodeMap = new TreeMap<>();
        Map<String, String> beginBigNodeMap = new TreeMap<>();
        Map<String, String> beginSmallNodeMap = new TreeMap<>();

        List<String> finishSmallNodeList = new ArrayList<>();

        for (NodeNewschedulesModel m : models) {
            //更新完成节点的开始时间跟结束时间(大节点)
            if ("1".equals(m.getType()) && "2".equals(m.getStatus()) && "1".equals(m.getState())) {
                //实际开始时间与实际结束时间
                finishBigNodeMap.put(m.getNodeId(), m.getFactFinishDate());
                beginBigNodeMap.put(m.getNodeId(), m.getPlanBeginDate());
            }
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd");
        String nowDate = format.format(getDate());
        for (NodeNewschedulesModel mo : models) {
            //确定的已完成的小节点都存起来
            if ("2".equals(mo.getStatus()) && "1".equals(mo.getState()) && "2".equals(mo.getType())) {
                finishSmallNodeMap.put(mo.getNodeId(), mo.getFactFinishDate());
            }
            //确定的未完成节点(小节点)
            if (!"2".equals(mo.getStatus()) && "1".equals(mo.getState()) && "2".equals(mo.getType())) {
                //如果该小节点处于进行中，则计划开始时间就是实际开始时间
                if ("1".equals(mo.getStatus())) {
                    String planBeginTime = mo.getPlanBeginDate();
                    if (StringUtils.isBlank(planBeginTime)) {
                        planBeginTime = nowDate;
                    }
                    String planTime = mo.getPlanTime();
                    Date today = format.parse(planBeginTime);
                    List<String> holidays = new ArrayList<>();
                    Date result = getScheduleActiveDate(today, holidays, Integer.parseInt(planTime) - 1);
                    String planFinishTime = format.format(result);
                    finishSmallNodeMap.put(mo.getNodeId(), planFinishTime);
                    mo.setPlanBeginTime(planBeginTime);
                    mo.setPlanEndTime(planFinishTime);
                    finishSmallNodeList.add(planFinishTime);
                } else {
                    //如果是第一个小节点才执行以下操作
                    if (mo.getNodeId().contains(".1")) {
                        //获取前置节点
                        String preNodeIds = (String) preOfNodeMap.get(mo.getNodeId());

                        //跳过23、 24节点，25节点的计划时间用22节点的计划时间推算
                        if (mo.getNodeId().contains("25")) {
                            preNodeIds = "22";
                        }

                        if (StringUtils.isNotBlank(preNodeIds)) {
                            if (!preNodeIds.contains("#")) {

                                if (preNodeIds.contains(",")) {
                                    List<String> finishTime = new ArrayList<>();
                                    String[] pres = preNodeIds.split(",");
                                    for (String ps : pres) {
                                        String fisT = finishBigNodeMap.get(ps);
                                        if (StringUtils.isBlank(fisT)) {
//                                            fisT = finishSmallNodeMap.get(((TreeMap<String, String>) finishSmallNodeMap).lastKey());
//                                    fisT = format.format(getDate());
                                            if (finishSmallNodeList.size() > 0) {
                                                fisT = finishSmallNodeList.get(finishSmallNodeList.size() - 1);
                                            } else {
                                                fisT = format.format(getDate());
                                            }

                                        }
                                        finishTime.add(fisT);
                                    }
                                    //排序finishTime获取最迟的时间
                                    Collections.sort(finishTime, new Comparator<String>() {
                                        public int compare(String p1, String p2) {
                                            SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd");
                                            int result = 0;
                                            if (p1.equals(p2)) {
                                                return result;
                                            }
                                            try {
                                                Date date1Str = format.parse(p1);
                                                Date date2Str = format.parse(p2);
                                                if (date1Str.after(date2Str)) {
                                                    result = -1;
                                                } else {
                                                    result = 1;
                                                }
                                            } catch (ParseException e) {
                                                e.printStackTrace();
                                            }
                                            return result;
                                        }
                                    });
                                    String planBeginTime = finishTime.get(0);
//                                    Date datePlanBeginTime = format.parse(planBeginTime);
//                                    Date dateNowDate = format.parse(nowDate);
//                                    if (dateNowDate.after(datePlanBeginTime)) {
//                                        planBeginTime = finishSmallNodeMap.get(((TreeMap<String, String>) finishSmallNodeMap).lastKey());
//                                    }
                                    String planTime = mo.getPlanTime();
                                    /*List<String> holidayDemo = new ArrayList<>();
                                    Date todayDemo = format.parse(planBeginTime);
                                    Date today = getScheduleActiveDate(todayDemo, holidayDemo, 1);
                                    planBeginTime = format.format(today);*/
                                    Date today = format.parse(planBeginTime);
                                    List<String> holidays = new ArrayList<>();
                                    Date result = getScheduleActiveDate(today, holidays, Integer.parseInt(planTime) - 1);
                                    String planFinishTime = format.format(result);
                                    finishSmallNodeMap.put(mo.getNodeId(), planFinishTime);
                                    beginSmallNodeMap.put(mo.getNodeId(), planBeginTime);

                                    //---------------------------------------------------
                                    mo.setPlanBeginTime(planBeginTime);
                                    mo.setPlanEndTime(planFinishTime);
                                    finishSmallNodeList.add(planFinishTime);
                                    String[] node_ = mo.getNodeId().split("\\.");
                                    finishBigNodeMap.put(node_[0], planFinishTime);
                                    System.out.println("小节点成功");
                                } else {
                                    String fisT = finishBigNodeMap.get(preNodeIds);
                                    String planBeginTime;
                                    if (StringUtils.isBlank(fisT)) {
                                        if (finishSmallNodeList.size() > 0) {
                                            planBeginTime = finishSmallNodeList.get(finishSmallNodeList.size() - 1);
                                        } else {
                                            planBeginTime = format.format(getDate());
                                        }

                                    } else {
                                        planBeginTime = fisT;
                                    }
                                    if (finishSmallNodeList.size() > 0) {
                                        Date datePlanBeginTime = format.parse(planBeginTime);
                                        String flagTime = finishSmallNodeList.get(finishSmallNodeList.size() - 1);
                                        Date dateNowDate = format.parse(flagTime);
                                        if (dateNowDate.after(datePlanBeginTime)) {
                                            planBeginTime = finishSmallNodeList.get(finishSmallNodeList.size() - 1);
                                        }
                                    }

                                    String planTime = mo.getPlanTime();
                                   /* List<String> holidayDemo = new ArrayList<>();
                                    Date todayDemo = format.parse(planBeginTime);
                                    Date today = getScheduleActiveDate(todayDemo, holidayDemo, 1);
                                    planBeginTime = format.format(today);*/
                                    Date today = format.parse(planBeginTime);
                                    List<String> holidays = new ArrayList<>();
                                    Date result = getScheduleActiveDate(today, holidays, Integer.parseInt(planTime) - 1);
                                    String planFinishTime = format.format(result);
                                    finishSmallNodeMap.put(mo.getNodeId(), planFinishTime);
                                    beginSmallNodeMap.put(mo.getNodeId(), planBeginTime);
                                    //---------------------------------------------------
                                    mo.setPlanBeginTime(planBeginTime);
                                    mo.setPlanEndTime(planFinishTime);
                                    finishSmallNodeList.add(planFinishTime);
                                    String[] node_ = mo.getNodeId().split("\\.");
                                    finishBigNodeMap.put(node_[0], planFinishTime);
                                    System.out.println("小节点成功");
                                }
                            } else {
                                String planBeginTime = nowDate;
                                mo.setPlanBeginTime(planBeginTime);
                                String planTime = mo.getPlanTime();
                                /*List<String> holidayDemo = new ArrayList<>();
                                Date todayDemo = format.parse(planBeginTime);
                                Date today = getScheduleActiveDate(todayDemo, holidayDemo, 1);
                                planBeginTime = format.format(today);*/
                                Date today = format.parse(planBeginTime);
                                List<String> holidays = new ArrayList<>();
                                Date result = getScheduleActiveDate(today, holidays, Integer.parseInt(planTime) - 1);
                                String planFinishTime = format.format(result);
                                mo.setPlanBeginTime(planBeginTime);
                                mo.setPlanEndTime(planFinishTime);
                                finishSmallNodeList.add(planFinishTime);
                                String[] node_ = mo.getNodeId().split("\\.");
                                finishBigNodeMap.put(node_[0], planFinishTime);
                                System.out.println("小节点成功");
                            }
                        }


                    } else {
                        String[] node_ = mo.getNodeId().split("\\.");
                        String node_v = Integer.parseInt(node_[1]) - 1 + "";
                        String planBeginTime = finishSmallNodeMap.get(mo.getNodeIds() + "." + node_v);
                        if (StringUtils.isBlank(planBeginTime)) {
                            node_v = Integer.parseInt(node_[1]) - 2 + "";
                            planBeginTime = finishSmallNodeMap.get(mo.getNodeIds() + "." + node_v);
                            if (StringUtils.isBlank(planBeginTime)) {
                                continue;
                            }
//                        continue;
                        }
                        String planTime = StringUtils.isBlank(mo.getPlanTime()) ? "1" : mo.getPlanTime();
                        List<String> holidays = new ArrayList<>();
                        /*List<String> holidayDemo = new ArrayList<>();
                        Date todayDemo = format.parse(planBeginTime);
                        Date today = getScheduleActiveDate(todayDemo, holidayDemo, 1);
                        planBeginTime = format.format(today);*/
                        Date today = format.parse(planBeginTime);
                        Date result = getScheduleActiveDate(today, holidays, Integer.parseInt(planTime) - 1);
                        String planFinishTime = format.format(result);
                        finishSmallNodeMap.put(mo.getNodeId(), planFinishTime);
                        beginSmallNodeMap.put(mo.getNodeId(), planBeginTime);

                        //更新完成的大节点的结束时间
                        finishBigNodeMap.put(node_[0], planFinishTime);
                        //---------------------------------------------------
                        mo.setPlanBeginTime(planBeginTime);
                        mo.setPlanEndTime(planFinishTime);
                        finishSmallNodeList.add(planFinishTime);
                        System.out.println("ok");
                    }
                }
            }
        }
        return models;
    }
}
